package com.sunfield.windpower.decoration;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.sunfield.windpower.R;
import com.sunfield.windpower.util.Tools;

/**
 * Created by wxl on 2017/2/8.
 */
public class MessageDecoration extends RecyclerView.ItemDecoration {
    Paint paint;
    public MessageDecoration(Context context) {
        paint = new Paint();
        paint.setColor(Tools.getColor(context, R.color.line_bg));
        paint.setStrokeWidth(2f);
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);

        final int childCount = parent.getChildCount();
        if(childCount == 0 || parent.getAdapter() == null){
            return;
        }
        int size = parent.getAdapter().getItemCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            int index = parent.getChildAdapterPosition(child);
            Rect mBounds = new Rect();
            parent.getDecoratedBoundsWithMargins(child, mBounds);
            int bottom = mBounds.bottom + Math.round(ViewCompat.getTranslationY(child));
            if(index == size - 2){
                c.drawLine(0, bottom, parent.getWidth(), bottom, paint);
            }else if(index < size - 2){
                c.drawLine(parent.getPaddingLeft(), bottom, parent.getWidth(), bottom, paint);
            }
        }
    }
}
